#define  _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 /**
  * Note: The returned array must be malloced, assume caller calls free().
  */
typedef struct TreeNode TreeNode;
int TreeSize(TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}

void PreorderTraversal(TreeNode* root, int* arr, int* pi)
{
    if (root == NULL)
    {
        return;
    }
    arr[(*pi)++] = root->val;
    PreorderTraversal(root->left, arr, pi);
    PreorderTraversal(root->right, arr, pi);

}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    *returnSize = TreeSize(root);
    int* Returnarr = (int*)malloc(sizeof(int) * (*returnSize));
    int i = 0;

    PreorderTraversal(root, Returnarr, &i);
    return Returnarr;
}